
template <class T>
class Engine
{

public:

//        static SDL_Surface *Screen;
        Sint64 screenx;
        Sint64 screeny;
        //SDL_Surface *Clear;
        sf::RenderWindow App;

        //sf::RenderTarget Accumulate;



        std::list <T> List;
        int screenwidth, screenheight;

        Engine(int pScreenWidth, int pScreenHeight) : List(), screenwidth(pScreenWidth), screenheight(pScreenHeight), App(sf::VideoMode(pScreenWidth, pScreenHeight, 32), "This will only end badly") {


                ///Initialise screen.
                /*if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
                        std::cout <<"Unable to initialize SDL: " << SDL_GetError() << std::endl;
                        exit(107);
                }

                atexit(SDL_Quit);

                Screen = SDL_SetVideoMode(screenwidth, screenheight, 32, SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_FULLSCREEN);
                if (Screen == NULL) {
                        std::cout << "Unable to set video mode: " << SDL_GetError() << std::endl;
                        exit(116);
                }*/

                screenx=0;
                screeny=0;
                //SDL_Surface *Clear;
                /*Clear=SDL_CreateRGBSurface(SDL_DOUBLEBUF  , pScreenWidth, pScreenHeight, 32, 0, 0, 0, 0);


                SDL_FillRect(Clear, NULL, 0x00000000);*/



                //delete Clear;
        }


        void remove_num(int Num) {
                typename std::list<T>::iterator it;
                it=List.begin();
                std::advance(it, Num);
                List.erase(it);
                //List.
        }

        int return_num() {
                return List.size();
        }

        ~Engine(){
        }

        //void add_clear_end(){
           // add_surface_end("", Clear, 0, 0, 0);

        //}





        void draw_all(int prx=0, int pry=0) {

            //SDL_BlitSurface(Clear, NULL, Screen, NULL);
            App.Clear();

                for(typename std::list<T>::iterator it=List.begin(); it!=List.end(); it++) {

                        //SDL_Surface *Draw=(*it).return_surface();
                       // SDL_Surface *Draw=(*it).Surface;

                       sf::Sprite *Draw=(*it).Sprite;

                        //if((*it).Transparency==1) {

                                //SDL_SetColorKey( Draw, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB(Draw->format, 255,0,255) );

                        //}

                        //SDL_Rect Source, Destination;



                        //Source=(*it).Src;
                        //Destination=(*it).Dest;

                        //Sint16 B=pow(2, 16)-1;
                        int B = (screenwidth>screenheight) ? screenwidth : screenheight;


                        Sint64 x, y;
                        x=(*it).Dest.x;
                        y=(*it).Dest.y;
                        //printf("Prex:%i\n", x);
                        if((*it).r!=1){
                        //Destination.x+=-prx;
                        //Destination.y+=-pry;
                        x-=prx;
                        y-=pry;
                        }



                        /*if(abs((*it).Src.x)<B){
                        Source.x
                        =(*it).Src.x;
                        }
                        if(abs((*it).Src.y)<B){
                        Source.y
                        =(*it).Src.y;
                        }
                        if(abs((*it).Src.w)<B){
                        Source.w
                        =(*it).Src.w;
                        }
                        if(abs((*it).Src.h)<B){
                        Source.h
                        =(*it).Src.h;
                        }

                        if(abs(x<B)){
                        Destination.x
                        =x;
                        }

                        if(abs(y<B)){
                        Destination.y
                        =y;
                        }
                        if(abs((*it).Dest.w)<B){
                        Destination.w
                        =(*it).Dest.w;
                        }
                        if(abs((*it).Dest.h)<B){
                        Destination.h
                        =(*it).Dest.h;
                        }*/




                        if(Draw==NULL){
                            exit(12345);
                        }
                        //if(Screen==NULL){
                        //    exit(54321);

                        //}

                        if(abs(x)<(pow((long double)2, (long double)16)-1) && abs(y)<(pow((long double)2, (long double)16)-1)){


                       /* Destination.x=x;
                        Destination.y=y;
                        Destination.h=(*it).Dest.h;
                        Destination.w=(*it).Dest.w;

                        Source.x=0;
                        Source.y=0;
                        Source.w=(*it).Src.w;
                        Source.h=(*it).Src.w;*/

                        Draw->SetX((float)x);
                        Draw->SetY((float)y);

                        //printf("D:%i",Destination.x);
                        //printf("Postx:%i\n", Destination.x);


                        //SDL_BlitSurface(Draw, &Source, Screen, &Destination);
                        //Accumulate.Draw(*Draw);
                        App.Draw(*Draw);




                        }


                }

        }

        void flip(){

            /*if(SDL_MUSTLOCK(Screen)){SDL_LockSurface(Screen);}
            SDL_Flip(Screen);
            //SDL_RenderPresent(Screen);
            if(SDL_MUSTLOCK(Screen)){
            SDL_UnlockSurface(Screen);
            }*/

            //App.Draw(Accumulate);
            //Accumulate.Display();
            App.Display();
        }

        void add_image_end(T t) {

                //t.SetX(x);
                //t.SetY(y);
                List.push_back(t);
        }


        void add_file_end(std::string Name,int x=0, int y=0,  int pTransparency=0) {

                T Load;
                Load.Load(Name, x, y, pTransparency);
                List.push_back(Load);

        }


        void remove_all(){

            List.clear();

        }


};

//template <class T> SDL_Surface * Engine<T>::Screen = 0;

